package com.yztc.love_travel.adater;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.LoopPagerAdapter;
import com.yztc.love_travel.R;

/**
 * Created by 周旭 on 2016/11/28.
 */

public class ADAdapter extends LoopPagerAdapter {

    private Drawable[] adDrawableArray; //轮播的广告的数组资源

    public ADAdapter(Context context,RollPagerView viewPager) {
        super(viewPager);
        initADData(context);
    }

    //初始化广告的数据源
    private void initADData(Context context) {
        TypedArray typedArray = context.getResources().obtainTypedArray(R.array.ad_icons);
        adDrawableArray = new Drawable[typedArray.length()];
        for (int i=0;i<typedArray.length();i++){
            //设置数据源
            adDrawableArray[i] = typedArray.getDrawable(i);
        }
    }

    @Override
    public View getView(ViewGroup container, int position) {

        ImageView view = new ImageView(container.getContext());
        view.setScaleType(ImageView.ScaleType.FIT_XY);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        view.setImageDrawable(adDrawableArray[position]);
        return view;
    }

    @Override
    public int getRealCount() {
        return adDrawableArray.length;
    }
}
